import { NextResponse } from 'next/server'

import { getCacheTime, getConfig } from '@/lib/config'
import { searchFromApi } from '@/lib/downstream'
import { yellowWords } from '@/lib/yellow'

export const runtime = 'nodejs'

export async function GET(request: Request) {
  const { searchParams } = new URL(request.url)
  const query = searchParams.get('q')

  if (!query) {
    const cacheTime = await getCacheTime()
    return NextResponse.json(
      { results: [] },
      {
        headers: {
          'Cache-Control': `public, max-age=${cacheTime}, s-maxage=${cacheTime}`,
          'CDN-Cache-Control': `public, s-maxage=${cacheTime}`,
          'Vercel-CDN-Cache-Control': `public, s-maxage=${cacheTime}`,
        },
      },
    )
  }

  const config = await getConfig()
  const apiSites = config.SourceConfig.filter(site => !site.disabled)
  const searchPromises = apiSites.map(site => searchFromApi(site, query))

  try {
    const results = await Promise.all(searchPromises)
    let flattenedResults = results.flat()
    if (!config.SiteConfig.DisableYellowFilter) {
      flattenedResults = flattenedResults.filter((result) => {
        const typeName = result.type_name || ''
        return !yellowWords.some((word: string) => typeName.includes(word))
      })
    }
    const cacheTime = await getCacheTime()

    return NextResponse.json(
      { results: flattenedResults },
      {
        headers: {
          'Cache-Control': `public, max-age=${cacheTime}, s-maxage=${cacheTime}`,
          'CDN-Cache-Control': `public, s-maxage=${cacheTime}`,
          'Vercel-CDN-Cache-Control': `public, s-maxage=${cacheTime}`,
        },
      },
    )
  }
  catch {
    return NextResponse.json({ error: '搜索失败' }, { status: 500 })
  }
}
